package com.zhongbei.controller;

import com.zhongbei.pojo.Study;
import com.zhongbei.service.StudyService;
import com.zhongbei.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/study")
public class StudyController {

    @Autowired
    private StudyService studyService;

    @PostMapping
    public Result<String> create(@RequestBody Study study) {
        int i = studyService.insertStudy(study);
        return i == 1 ? Result.success("新增成功") : Result.error("新增失败");
    }

    @DeleteMapping("/{studyId}")
    public Result<String> delete(@PathVariable Integer studyId) {
        int i = studyService.deleteStudy(studyId);
        return i == 1 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PutMapping("/{studyId}")
    public Result<String> update(@PathVariable Integer studyId, @RequestBody Study study) {
        study.setStudyId(studyId);
        int i = studyService.updateStudy(study);
        return i == 1 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping
    public Result<List<Study>> listAll() {
        return Result.success(studyService.selectStudy());
    }

    @GetMapping("/{studyId}")
    public Result<Study> getById(@PathVariable Integer studyId) {
        return Result.success(studyService.selectStudyByStudyId(studyId));
    }

    @GetMapping("/category/{studyCategory}")
    public Result<List<Study>> getByCategory(@PathVariable Integer studyCategory) {
        return Result.success(studyService.selectStudyByStudyCategory(studyCategory));
    }

    @GetMapping("/mandatory/{isMandatory}")
    public Result<List<Study>> getByMandatory(@PathVariable Integer isMandatory) {
        return Result.success(studyService.selectStudyByIsMandatory(isMandatory));
    }
}
